package com.nhjf.mobile.controller.courses;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bson.util.StringRangeSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.CoursesType;
import com.nhjf.model.teachers.TeacherHasCoursesType;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import com.nhjf.pojo.qo.teachers.CoursesTypeQo;
import com.nhjf.pojo.qo.teachers.TeacherHasCoursesTypeQo;
import com.nhjf.service.teachers.CoursesTypeService;
import com.nhjf.service.teachers.TeacherHasCoursesTypeService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/coursesType")
public class CoursesTypeController {

	@Autowired
	private CoursesTypeService coursesTypeService;

	@Autowired
	private TeacherHasCoursesTypeService teacherHasCoursesTypeService;
	/**
	 * 查询课程类型
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/coursesType_view")
	public String showType(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {

			CoursesTypeQo qo=new CoursesTypeQo();
			qo.setType(1);

			List<CoursesType> coursesTypeList = coursesTypeService.queryList(qo);

			if(coursesTypeList!=null) {
				resMap.put("code", "200");
				resMap.put("message", coursesTypeList);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "500");
				resMap.put("message", "系统异常！");
				return JSONObject.fromObject(resMap).toString();
			}              
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		resMap.put("code", "500");
		resMap.put("message", "系统异常!");
		return JSONObject.fromObject(resMap).toString();
	}
	/**
	 * 根据pid查询课程类型
	 * @param request
	 * @param response
	 * @param model
	 * @param pId
	 * @return
	 */
	@RequestMapping("/coursesType_Subctype")
	@ResponseBody
	public String showSubctype(HttpServletRequest request, HttpServletResponse response, Model model,String pId) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			CoursesTypeQo coursesTypeQo=new CoursesTypeQo();
			coursesTypeQo.setpId(Long.parseLong(pId));
			coursesTypeQo.setType(2);
			List<CoursesType> list=coursesTypeService.queryList(coursesTypeQo);
			if(list!=null) {
				resMap.put("code", "200");
				resMap.put("message", list);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "404");
				resMap.put("message", "没有找到此类型!");
				return JSONObject.fromObject(resMap).toString();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常!");
			return JSONObject.fromObject(resMap).toString();
		}
	}

	/**
	 * 查询老师的所属专业
	 * @param request
	 * @param response
	 * @param model
	 * @param type
	 * @return
	 */
	@RequestMapping("/coursesType_showTeacherType")
	@ResponseBody
	public String showTeacherType(HttpServletRequest request, HttpServletResponse response, Model model,String type,@RequestParam(value = "page", defaultValue = "1")Integer page ) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		try {
			TeacherHasCoursesTypeQo teacherHasCoursesTypeQo=new TeacherHasCoursesTypeQo();
			teacherHasCoursesTypeQo.setSeachTeacher(true);
			teacherHasCoursesTypeQo.setCoursesTypeId(Long.parseLong(type));
			Pagination pagination = new Pagination();
			pagination.setPageSize(10);
			pagination.setPageNo(page);
			pagination.setCondition(teacherHasCoursesTypeQo);
			pagination = teacherHasCoursesTypeService.queryPagination(pagination);
			List<TeacherHasCoursesType> list=(List<TeacherHasCoursesType>)pagination.getList();
			
			SysProperties sysProperties=SysProperties.getInstance();
			String imagePath=sysProperties.get("imagePath");
			for (TeacherHasCoursesType teacherHasCoursesType : list) {
				TeacherHasCoursesTypeQo teacherHasCoursesTypeQo1=new TeacherHasCoursesTypeQo();
				teacherHasCoursesTypeQo1.setTeacherId(teacherHasCoursesType.getTeacherId().getId());
				teacherHasCoursesTypeQo1.setSeachCoursesType(true);
				List<TeacherHasCoursesType> typeList=teacherHasCoursesTypeService.queryList(teacherHasCoursesTypeQo1);
				String typeListString="";
				for (TeacherHasCoursesType cType : typeList) {
					typeListString+=cType.getCoursesTypeId().getCoursesType()+",";
				}
				teacherHasCoursesType.setTeacherTypeList(typeListString);
				if(teacherHasCoursesType.getTeacherId().getAvatar().indexOf(imagePath+"/picture/")>=0) {
					
				}else {
					teacherHasCoursesType.getTeacherId().setAvatar(imagePath+"/picture/"+teacherHasCoursesType.getTeacherId().getAvatar());
				}
				
			}
			if(list!=null) {
				resMap.put("code", "200");
				resMap.put("message", pagination);
				return JSONObject.fromObject(resMap).toString();
			}else {
				resMap.put("code", "404");
				resMap.put("message", "未找到此类型!");
				return JSONObject.fromObject(resMap).toString();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resMap.put("code", "500");
			resMap.put("message", "系统异常！");
			return JSONObject.fromObject(resMap).toString();
		}
	}
}
